﻿using System;
using System.Data;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GiaoDien
{
    public partial class FormHoSoNhanVien : Form
    {
        private DataSet dataset;
        private string strquery;
        public FormHoSoNhanVien()
        {
            InitializeComponent();
        }
        private void init()
        {
            dataset = new DataSet();
            DataTable datatablebp = new DataTable();
            dataset.Tables.Add(datatablebp);
            dataset.Tables[0].TableName = "bophan";
            DataTable datatablebc = new DataTable();
            dataset.Tables.Add(datatablebc);
            dataset.Tables[1].TableName = "bangcap";
            DataTable datatablecv = new DataTable();
            dataset.Tables.Add(datatablecv);
            dataset.Tables[2].TableName = "chucvu";
            DataTable datatablett = new DataTable();
            dataset.Tables.Add(datatablett);
            dataset.Tables[3].TableName = "tinhtrang";
            DataTable datatablenv = new DataTable();
            dataset.Tables.Add(datatablenv);
            dataset.Tables[4].TableName = "nhanvien";
        }
        private void loadbophan()
        {
            BOPHANBUS bpBUS = new BOPHANBUS();
            DataTable datatable = new DataTable();
            datatable = bpBUS.Loadbophan();
            dataset.Tables["bophan"].Clear();
            dataset.Tables["bophan"].Merge(datatable);
            cbbbophan.DataSource = dataset.Tables["bophan"];
            cbbbophan.DisplayMember = "TenBoPhan";
            cbbbophan.ValueMember = "MaBoPhan";
            if (dataset.Tables["bophan"].Rows.Count != 0)
                cbbbophan.SelectedIndex = 0;
        }
        private void Loadbangcap()
        {
            BANGCAPBUS bcBUS = new BANGCAPBUS();
            DataTable datatable = new DataTable();
            datatable = bcBUS.Loadbangcap();
            dataset.Tables["bangcap"].Clear();
            dataset.Tables["bangcap"].Merge(datatable);
            cbbbangcap.DataSource = dataset.Tables["bangcap"];
            cbbbangcap.DisplayMember = "TenBangCap";
            cbbbangcap.ValueMember = "MaBangCap";
            if (dataset.Tables["bangcap"].Rows.Count != 0)
                cbbbangcap.SelectedIndex = 0;
        }
        private void Loadchucvu()
        {
            CHUCVUBUS cvBUS = new CHUCVUBUS();
            DataTable datatable = new DataTable();
            datatable = cvBUS.Loadchucvu();
            dataset.Tables["chucvu"].Clear();
            dataset.Tables["chucvu"].Merge(datatable);
            cbbchucvu.DataSource = dataset.Tables["chucvu"];
            cbbchucvu.DisplayMember = "TenChucVu";
            cbbchucvu.ValueMember = "MaChucVu";
            if (dataset.Tables["chucvu"].Rows.Count != 0)
                cbbchucvu.SelectedIndex = 0;
        }
        private void Loadtinhtrang()
        {
            TINHTRANGBUS ttBUS = new TINHTRANGBUS();
            DataTable datatable = new DataTable();
            datatable = ttBUS.Loadtinhtrang();
            dataset.Tables["tinhtrang"].Clear();
            dataset.Tables["tinhtrang"].Merge(datatable);
            cbbtinhtrang.DataSource = dataset.Tables["tinhtrang"];
            cbbtinhtrang.DisplayMember = "TenTinhTrang";
            cbbtinhtrang.ValueMember = "MaTinhTrang";
            if (dataset.Tables["tinhtrang"].Rows.Count != 0)
                cbbtinhtrang.SelectedIndex = 0;
        }
        private void Loadnhanvien()
        {
            NHANVIENBUS nvBUS = new NHANVIENBUS();
            DataTable datatable = new DataTable();
            datatable = nvBUS.Bangnhanvien();
            dataset.Tables["nhanvien"].Clear();
            dataset.Tables["nhanvien"].Merge(datatable);
        }
        private void Manhanvien()
        {
            NHANVIENBUS nvBUS = new NHANVIENBUS();
            string mabophan = cbbbophan.SelectedValue.ToString().Trim();
            if (mabophan != "")
            {
                txbmanhanvien.Text = "";
                txbmanhanvien.Text = nvBUS.Manhanvien(mabophan);
            }
        }
        private void Loadngaysinh()
        {
            for (int i = 1; i <= 31; i++)
                cbbngaysinh.Items.Add(i);
            cbbngaysinh.SelectedIndex = 0;
        }
        private void Loadthangsinh()
        {
            for (int i = 1; i <= 12; i++)
                cbbthangsinh.Items.Add(i);
            cbbthangsinh.SelectedIndex = 0;
        }
        private void Loadnamsinh()
        {
            int yearstart = DateTime.Now.Year - 60;
            for (int i = yearstart; i <= DateTime.Now.Year; i++)
                cbbnamsinh.Items.Add(i);
            cbbnamsinh.SelectedIndex = 0;
        }       
        private void xemtatca()
        {
            Loadnhanvien();
            girdbophan.DataSource = dataset.Tables["bophan"];
            girdbophan.DisplayMember = "TenBoPhan";
            girdbophan.ValueMember = "MaBoPhan";
            girdchucvu.DataSource = dataset.Tables["chucvu"];
            girdchucvu.DisplayMember = "TenChucVu";
            girdchucvu.ValueMember = "MaChucVu";
            girdbangcap.DataSource = dataset.Tables["bangcap"];
            girdbangcap.DisplayMember = "TenBangCap";
            girdbangcap.ValueMember = "MaBangCap";
            girdtinhtrang.DataSource = dataset.Tables["tinhtrang"];
            girdtinhtrang.DisplayMember = "TenTinhTrang";
            girdtinhtrang.ValueMember = "MaTinhTrang";
            dataGridView1.DataSource = dataset.Tables["nhanvien"];
        }
        private void FormHoSoNhanVien_Load(object sender, EventArgs e)
        {
            try
            {
                init();
                loadbophan();
                Loadngaysinh();
                Loadthangsinh();
                Loadnamsinh();
                Loadbangcap();
                Loadchucvu();
                Loadtinhtrang();
                Manhanvien();
                Loadnhanvien();
                xemtatca();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnlapthe_Click(object sender, EventArgs e)
        {
            NHANVIENDTO nvDTO = new NHANVIENDTO();
            nvDTO.Manhanvien = this.txbmanhanvien.Text.Trim();
            nvDTO.Hoten = this.txbhoten.Text.Trim();
            int day = Convert.ToInt32(this.cbbngaysinh.SelectedItem.ToString());
            int month = Convert.ToInt32(this.cbbthangsinh.SelectedItem.ToString());
            int year = Convert.ToInt32(this.cbbnamsinh.SelectedItem.ToString());
            nvDTO.Ngaysinh = new DateTime(year, month, day);

            nvDTO.Diachi = this.txbdiachi.Text.Trim();
            nvDTO.Dienthoai = this.txbdienthoai.Text.Trim();
            nvDTO.Mabangcap = this.cbbbangcap.SelectedValue.ToString();
            nvDTO.Mabophan = this.cbbbophan.SelectedValue.ToString();
            nvDTO.Machucvu = this.cbbchucvu.SelectedValue.ToString();
            nvDTO.Tinhtrang = this.cbbtinhtrang.SelectedValue.ToString();
            NHANVIENBUS nvBUS = new NHANVIENBUS();
            int kq = nvBUS.Themnhanvien(nvDTO);
            if (kq > 0)
            {
                MessageBox.Show("Thêm thành công");
                xemtatca();
            }
            else MessageBox.Show("Thêm thất bại");
        }    
        private void cbbbophan_SelectedIndexChanged(object sender, EventArgs e)
        {
            Manhanvien();
        }
        private void btnthemoi_Click(object sender, EventArgs e)
        {
            cbbbophan.SelectedIndex = 0;
            txbhoten.Text = "";
            cbbngaysinh.SelectedIndex = 0;
            cbbthangsinh.SelectedIndex = 0;
            cbbnamsinh.SelectedIndex = 0;
            txbdiachi.Text="";
            txbdienthoai.Text = "";
            cbbbangcap.SelectedIndex = 0;
            cbbchucvu.SelectedIndex = 0;
            cbbtinhtrang.SelectedIndex = 0;
        }     
        private void btnxemtatca_Click(object sender, EventArgs e)
        {
            xemtatca();
        }
        private void capnhat()
        {
            NHANVIENBUS nvBUS = new NHANVIENBUS();
            strquery = "SELECT * FROM NHAN_VIEN";
            int kq = nvBUS.update(strquery, dataset);
            if (kq > 0)
            {
                MessageBox.Show("Thành công");
            }
            else
                MessageBox.Show("Thất bại");
        }
        private void btncapnhat_Click(object sender, EventArgs e)
        {
            capnhat();
        }
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btntracuu_Click(object sender, EventArgs e)
        {
            if (txbmanhanvien2.Text != "")
            {
                DataView dataview = new DataView(dataset.Tables["nhanvien"]);
                string filter = "MaNhanVien = '" + txbmanhanvien2.Text + "'";
                dataview.RowFilter = filter;
                this.dataGridView1.DataSource = dataview;
            }
            else if (txbhoten2.Text != "")
            {
                DataView dataview = new DataView(dataset.Tables["nhanvien"]);
                string filter = "HoTen like '%" + txbhoten2.Text + "%'";
                dataview.RowFilter = filter;
                this.dataGridView1.DataSource = dataview;
            }
        }
        private void xem()
        {
            girdbophan.DataSource = dataset.Tables["bophan"];
            girdbophan.DisplayMember = "TenBoPhan";
            girdbophan.ValueMember = "MaBoPhan";
            girdchucvu.DataSource = dataset.Tables["chucvu"];
            girdchucvu.DisplayMember = "TenChucVu";
            girdchucvu.ValueMember = "MaChucVu";
            girdbangcap.DataSource = dataset.Tables["bangcap"];
            girdbangcap.DisplayMember = "TenBangCap";
            girdbangcap.ValueMember = "MaBangCap";
            girdtinhtrang.DataSource = dataset.Tables["tinhtrang"];
            girdtinhtrang.DisplayMember = "TenTinhTrang";
            girdtinhtrang.ValueMember = "MaTinhTrang";
            Loadnhanvien();
            DataView dataview = new DataView(dataset.Tables["nhanvien"]);
            string filter = "TinhTrang = 'HĐ'";
            dataview.RowFilter = filter;
            this.dataGridView1.DataSource = dataview;
        }
        private void btnxem_Click(object sender, EventArgs e)
        {
            xem();
        }
        private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DialogResult kq = MessageBox.Show("bạn chắc không?", "nguyen", MessageBoxButtons.OKCancel);
                if (kq == DialogResult.OK)
                {
                    capnhat();
                }
                else
                {
                    xem();
                }
            }
        }
    }
}
